﻿var i; //interval on global scope
var t; //timeout on global scope
var start = function () {
    i = setInterval(function () { console.log(Date.now()); }, 3000);

};

var clearTI = function () {
    clearInterval(i);
};

var startTimeOut = function () {
    t = setTimeout(function () { startTimeOut(); console.log("timeout!"); }, 3000);
};
var clearTO = function () {
    clearTimeout(t);

};

//Wrapper
var superHeroes = {};
//Array 
superHeroes.members = [];
//constructor
superHeroes.Member = function (name, power, image) {
    this.name = name;
    this.power = power;
    this.image = image;
   
};
//prototype for Member
superHeroes.Member.prototype.isVillain = false;

superHeroes.drawTable = function () {
    var h = "<div class='container'>";
    h += "<div class='row'>";
    for (var i in superHeroes.members) {
        var currentMember = superHeroes.members[i];
        h += "<div class='col-md-4'>";
        h += "<h1>";
        h += "<i class='" + superHeroes.getIconName(currentMember.power) + "'></i> ";
        h += currentMember.name + "</h1><br/>";
        h += "<img src='" + currentMember.image + "' class='img-responsive img-thumbnail hero-image'/><br/>";
        h += "<div class='btn btn-danger fa fa-recycle' onclick='superHeroes.scrapMembers(" + i + ")'></div>";
        h += "<div class='btn btn-warning fa fa-edit' onclick='superHeroes.showEdit(" + i + ")'></div>"
        h += "</div>";
    }
    h += "</div>";
    h += "</div>";
    document.getElementById("tableHolder").innerHTML = h;
};

//Helper Function to pull Icon Names
superHeroes.getIconName = function (power) {
    if (power === "Everything") {
        return "fa fa-qq";
    }
    else if (power === "Speed") {
        return "fa fa-reddit";
    }
    else {
        return "fa fa-paw";
    }

};

//Add member to Array
superHeroes.addMember = function () {
    "use strict";
    var name, power, image;
    name = document.getElementById("name");
    power = document.getElementById("power");
    image = document.getElementById("image");
    var s = new superHeroes.Member(name.value, power.value, image.value);
    superHeroes.members.push(s);
    image.value = "";
    name.value = "";
    power.value = "";
    superHeroes.drawTable();
    superHeroes.sendHeroes(s);
};
//Delete member
superHeroes.scrapHeroes = function (index) {
    superHeroes.members.splice(index, 1);
    superHeroes.drawTable();
};
//Show Edit 
superHeroes.showEdit = function (index) {
    var member = superHeroes.members[index];
    document.getElementById("editTitle").innerHTML = "Hero information " + member.name;
    document.getElementById("editName").value = member.name;
    document.getElementById("editImage").value = member.image;
    document.getElementById("editPower").value = member.power;
    document.getElementById("isVillain").value = member.isVillain.toString();
    document.getElementById("saveChangesBtn").onclick = function () { superHeroes.saveEdit(index); };
    $("#editModal").modal();

};
//Save Edit 
superHeroes.saveEdit = function (index) {
    var member = superHeroes.members[index];
    member.name = document.getElementById("editName").value;
    member.image = document.getElementById("editImage").value;
    member.power = document.getElementById("editPower").value;
    if (document.getElementById("isVillain").value === "true") {
        member.isVillain = true;
    }
    else {
        delete member.isVillain;
    }
    //superHeroes.members[index].name = document.getElementById("editName").value;
    $("#editModal").modal("hide");
    superHeroes.drawTable();
};
/************************* AJAX ***********************/
superHeroes.sendHeroes = function (member) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "https://brilliant-fire-2547.firebaseio.com/.json", true);
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            alert(this.response);
            // setTimeout(sendHeroes, 3000)
        }
        //   else {
        //       console.log(" Error" + this.status + ":" + this.response);
        //   }
    };
    xhr.onerror = function () {
        alert("Comm Error...resending in 15s");
        // setTimeout(sendHeroes, 15000);
    };
    xhr.send(JSON.stringify(member));

};

superHeroes.getHeroes = function () {

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://brilliant-fire-2547.firebaseio.com/.json", true);
    xhr.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            console.log(this.response);
            setTimeout(callback, 3000);
        }
        else {
            console.log(this.response);
        }
        var data = JSON.parse(this.response);
        for (var x in data) {
            data[x].__proto__ = superHeroes.members.prototype;
            data[x].key = x;
            superHeroes.members.push(data[x]);
        }
        superHeroes.drawTable();
    };
    xhr.onerror = function () {
        console.log("Comm Error...resending in 15s");
        setTimeout(callback, 15000);
    };
    xhr.send(JSON.stringify());
}
/************************* AJAX ***********************/
//Seeder
superHeroes.seed = function () {
    superHeroes.members.push(new superHeroes.Member("Superman", "Everything", "http://www.hypergeeky.com/wp-content/uploads/2013/08/Superman-in-Superman-Unchained-2.png"));
    superHeroes.members.push(new superHeroes.Member("Batman", "Intelligence", "http://cdn.screenrant.com/wp-content/uploads/Batsuit-Reveal-Coming-for-Batman-vs-Superman.jpg"));
    superHeroes.members.push(new superHeroes.Member("The Flash", "Super Speed", "http://nerdreactor.com/wp-content/uploads/2013/05/character_bio_576_flash.jpg"));
    superHeroes.members.push(new superHeroes.Member("Spider-Man", "Everything", "http://cf.badassdigest.com/_uploads/images/27096/ultimate-spiderman__span.jpg"));
    superHeroes.members.push(new superHeroes.Member("The Hulk", "Super Strength", "http://www.comicbookmovie.com/images/users/uploads/12912/Hulk_004.jpg"))
    superHeroes.drawTable();
};
//Clear All
superHeroes.clearAll = function () {
    superHeroes.members = [];
    superHeroes.drawTable();
};
superHeroes.seed();
superHeroes.getHeroes();